//
//  FPBankCardDetailController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/3/13.
//

#import "FPBankCardDetailController.h"
#import "FPBankCardDetailFooterView.h"

@interface FPBankCardDetailController () <HJCActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UIView *backView;

@property (weak, nonatomic) IBOutlet UIImageView *backImgView;

@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UIView *logoContentView;
@property (weak, nonatomic) IBOutlet UIImageView *logoImgView;

@property (weak, nonatomic) IBOutlet UILabel *lab1;

@property (nonatomic , strong) FPBankCardDetailFooterView *footerView;
@property (weak, nonatomic) IBOutlet UIImageView *hideImgView;
@property (nonatomic , assign) BOOL showNumbers;

@end

@implementation FPBankCardDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.showNumbers = NO;
}

- (void)initUI {
    
    [self setTitle:@"我的银行卡" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    
    self.logoContentView.clipsToBounds = YES;
    self.logoContentView.layer.cornerRadius = 18.f;
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    
    self.footerView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPBankCardDetailFooterView.class) owner:nil options:nil].lastObject;
    [self.view addSubview:self.footerView];
    self.footerView.frame = CGRectMake(0.f, self.backImgView.bottom + 50.f + UIDevice.navigationFullHeight, SCREEN_WIDTH, 44.f);
    
    [self.footerView.addBtn setTitle:@"解除绑定" forState:(UIControlStateNormal)];
    self.footerView.addBtn.imageView.hidden = YES;
    [self.footerView.addBtn setImage:nil forState:(UIControlStateNormal)];
    self.lab1.adjustsFontSizeToFitWidth = YES;
    
    if (self.model.imagePath.length) {
     
        [self.logoImgView sd_setImageWithURL:[NSURL URLWithString:[FPUserModel.globalHeadURL stringByAppendingString:self.model.imagePath]] placeholderImage:IMG(@"icon_bank_card_default")];
    }
    else {
        
        self.logoImgView.image = IMG(@"icon_bank_card_default");
    }
    self.nameLab.text = self.model.bankName;
//    self.lab4.text = [self.model.bankCard substringFromIndex:self.model.bankCard.length - 4];
    
    self.lab1.text = [self formatBankCardNumber:self.model.bankCard mask:YES];
    
    [self.footerView.addBtn handleViewControlEvents:UIControlEventTouchUpInside withBlock:^{
       
        // 解除银行卡
        HJCActionSheet *sheet = [[HJCActionSheet alloc] initWithDelegate:self CancelTitle:@"取消" OtherTitles:@"确认解除", nil];
        [sheet show];
        
        // 通知列表刷新
        // ...
    }];
    
    [self.hideImgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideNumberAction)]];
}

- (void)hideNumberAction {
    
    self.showNumbers = !self.showNumbers;
    
    if (self.showNumbers) {
        
        self.hideImgView.image = IMG(@"icon_bank_card_number_hide");
//        self.lab1.text = [self.model.bankCard substringWithRange:NSMakeRange(0, 4)];
//        self.lab2.text = [self.model.bankCard substringWithRange:NSMakeRange(5, 8)];
//        self.lab3.text = [self.model.bankCard substringWithRange:NSMakeRange(9, 12)];
//        self.lab4.text = [self.model.bankCard substringFromIndex:self.model.bankCard.length - 4];
        self.lab1.text = [self formatBankCardNumber:self.model.bankCard mask:NO];
    }
    else {
        
        self.hideImgView.image = IMG(@"icon_bank_card_number_show");
//        self.lab4.text = [self.model.bankCard substringFromIndex:self.model.bankCard.length - 4];
        self.lab1.text = [self formatBankCardNumber:self.model.bankCard mask:YES];
    }
}


- (NSString *)formatBankCardNumber:(NSString *)cardNumber mask:(BOOL)mask {
    
    NSInteger totalLength = cardNumber.length;
    NSInteger groupSize = ceil(totalLength / 4.0);
    NSMutableArray *parts = [NSMutableArray array];

    for (int i = 0; i < 4; i++) {
        NSInteger start = i * groupSize;
        if (start >= totalLength) break;

        NSInteger length = MIN(groupSize, totalLength - start);
        NSString *part = [cardNumber substringWithRange:NSMakeRange(start, length)];

        if (mask && i < 3) {
            // 替换前三段为 ****（保持原始长度一致）
            NSString *masked = [@"" stringByPaddingToLength:part.length withString:@"*" startingAtIndex:0];
            [parts addObject:masked];
        } else {
            [parts addObject:part];
        }
    }

    return [parts componentsJoinedByString:@" "];
}


- (void)actionSheet:(HJCActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) {
        
        [MBManager showLoading];
        [FPBankCardModel deleteBankCardWithID:self.model.ID success:^(NSInteger code, NSString * _Nonnull message) {
            [MBManager hideLoading];
            
            if (code == 200) {
                
                [self showText:@"已解除绑定"];
                [[NSNotificationCenter defaultCenter] postNotificationName:FPBankCardAddSuccessNotification object:nil];
                [self performSelector:@selector(backAction) withObject:nil afterDelay:1.5f];
            }
            else {
                
                [self showText:message];
            }
            
        } failure:^(NSError * _Nonnull error) {
            [self showError:error];
        }];
    }
}




@end
